﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="LojaVirtual.Site.Styles.Site.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Login</title>
	<link href="../../Styles/Site/cssLogin.min.css" rel="stylesheet" />
	<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
	<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
	<form id="form1" runat="server">
		<asp:ScriptManager runat="server" ID="script" EnablePageMethods="true"></asp:ScriptManager>

		<asp:PlaceHolder runat="server" ID="placeholder"></asp:PlaceHolder>

		<section id="sectionPage">
			<header id="pageHead">
				<article id="imagem"></article>
			</header>
			<section id="pageBody">
				<section id="telaLogin">
					<article id="pageLogin">
						<div id="logo"></div>
					</article>
					<article id="camposLogin">
						<article id="campos">
							<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="camposTotal">
								<ContentTemplate>
									<table id="tabela">
										<tr>
											<td>
												<asp:Label ID="lblEmail" runat="server"></asp:Label></td>
											<td>
												<asp:TextBox runat="server" ID="txtEmail" CssClass="textBox"></asp:TextBox></td>
										</tr>
										<tr>
											<td>
												<asp:Label runat="server" ID="lblSenha"></asp:Label></td>
											<td>
												<asp:TextBox runat="server" ID="txtSenha" TextMode="Password" CssClass="textBox"></asp:TextBox></td>
										</tr>

										<tr>

											<td colspan="2">
												<asp:LinkButton runat="server" ID="lnkCadastrar" CssClass="link" OnClick="lnkNovoUser_Click"></asp:LinkButton>

												<asp:Button runat="server" ID="btnEntrar" CssClass="Entrar" OnClick="btnEntrar_Click" /></td>
										</tr>
										<tr>
											<td colspan="2" style="text-align: right">
												<div id="auth-status">
													<div id="auth-loggedout">
														<div class="fb-login-button" autologoutlink="true" scope="email,user_checkins">Login with Facebook</div>
													</div>
													<div id="auth-loggedin" style="display: none">
														Hi, <span id="auth-displayname"></span>(<a href="#" id="auth-logoutlink">logout</a>)
													</div>
												</div>
											</td>
										</tr>
									</table>
									<div id="camposFacebook">
										<asp:Label runat="server" ID="lblNome"></asp:Label>
										<asp:Label runat="server" ID="lblEmailFace"></asp:Label>
									</div>
								</ContentTemplate>
							</asp:UpdatePanel>
						</article>
					</article>
				</section>
			</section>
			<footer id="pageFooter">
			</footer>
		</section>


		<!--				JAVASCRIPT				 -->
		<script src="../../Scripts/jquery-1.11.1.min.js"></script>

		<script>
			//Quando inicia tem que fazer o logout

			$(document).ready(function () {
				$('#sectionPage').css('height', (screen.availHeight) + 'px');
			});

			// Load the SDK Asynchronously
			(function (d) {
				var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
				if (d.getElementById(id)) { return; }
				js = d.createElement('script'); js.id = id; js.async = true;
				js.src = "//connect.facebook.net/en_US/all.js";
				ref.parentNode.insertBefore(js, ref);
			}(document));

			// Init the SDK upon load
			window.fbAsyncInit = function () {
				FB.init({
					appId: '572954889505756', // App ID
					channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
					status: true, // check login status
					cookie: true, // enable cookies to allow the server to access the session
					xfbml: true  // parse XFBML
				});

				FB.getLoginStatus(checkLoginStatus);


				FB.login(checkLoginStatus,
					{ scope: 'email,user_likes,public_profile,publish_actions,publish_stream,friends_about_me,user_birthday,user_about_me,user_hometown,user_events' });

				//Connected: pega as infos e joga pro pop up e redireciona para home.
				//not_authorized: faz logoff e recarrega

				function checkLoginStatus(response) {
					if (response && response.status == 'not_authorized') {
						FB.logout(function () { window.location.reload(); });
						//alert(response.authResponse.accessToken);
					}
					//if (response && response.status == 'connected') {
					//    uid = response.authResponse.userID;
					//    token = response.authResponse.accessToken;
					//}
				}
				// listen for and handle auth.statusChange events
				FB.Event.subscribe('auth.statusChange', function (response) {
					if (response.authResponse) {
						// user has auth'd your app and is logged into Facebook
						FB.api('/me', function (me) {
							if (me.name) {
								//Abrir o pop up e carregar as infos do usuário, nome e email verificando se tem no sistema.                                

								//document.getElementById('auth-displayname').innerHTML = me.name;

								////Colocar labels que recebem esses valores e depois add no banco
								//$('#lblNome').text(me.name);
								//$('#lblEmailFace').text(me.email);

								//var nome = document.getElementById('lblNome').innerHTML;
								//var email = document.getElementById('lblEmailFace').innerHTML;
								//alert(me.authResponse.acessToken);
								window.location = "http://localhost:9779/Site/Pages/Site/UsuarioFace.aspx?Nome=" + me.name + "&Email=" + me.email;

								//$.ajax({
								//    type: "POST",
								//    url: "Login.aspx/CriaUsuario",
								//    data: "{" + nome + ", " + email + "}",
								//    contentType: "application/json; charset=utf-8",
								//    dataType: "json",
								//    success: function (msg) {
								//        alert("entrou");
								//        // Replace the div's content with the page method's return.
								//        $("#Result").text(msg.d);
								//    }
								//});

								//PageMethods.CriaUsuario(nome, email);
							}
						})
						//document.getElementById('auth-loggedout').style.display = 'none';
						//document.getElementById('auth-loggedin').style.display = 'block';
					}// else {
					//    // user has not auth'd your app, or is not logged into Facebook
					//    document.getElementById('auth-loggedout').style.display = 'block';
					//    document.getElementById('auth-loggedin').style.display = 'none';
					//}
				});

				//$("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });
			}
		</script>
	</form>
</body>
</html>
